package chapter_fiveteen.p214;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * Data time:2021/11/2 17:26
 * StudentID:2019112118
 * Author:hgw
 * Description: 客户端测试类
 */
public class Client {
    public static void main(String[] args) {
        InvocationHandler handler = null;
        AbstractUserDAO userDAO = new UserDAO();
        handler = (InvocationHandler) new DAOLogHandler(userDAO);
        AbstractUserDAO proxy = null;

        //动态创建代理对象，用于代理一个 AbstractUserDAO 类型的真实主题对象
        proxy = (AbstractUserDAO)Proxy.newProxyInstance(AbstractUserDAO.class.getClassLoader(),
                new Class[]{AbstractUserDAO.class}, handler);
        proxy.findUserById("张无忌");      //调用代理对象的业务方法

        System.out.println("----------------------------------");

        AbstractDocumentDAO docDAO = new DocumentDAO();
        handler = (InvocationHandler) new DAOLogHandler(docDAO);
        AbstractDocumentDAO proxy_new = null;

        //动态创建代理对象，用于代理一个AbstractDocumentDAO类型的真实主题对象
        proxy_new = (AbstractDocumentDAO) Proxy.newProxyInstance(AbstractDocumentDAO.class.getClassLoader(),
                new Class[]{AbstractDocumentDAO.class}, handler);
        proxy_new.deleteDocumentById("D002");       //调用代理对象的业务方法
    }
}
